package com.shangqing.system.droughtinformation.service.impl;

import com.shangqing.system.droughtinformation.entity.City;
import com.shangqing.system.droughtinformation.entity.DroughtGrade;
import com.shangqing.system.droughtinformation.entity.DroughtWarning;
import com.shangqing.system.droughtinformation.entity.Droughtinformation;
import com.shangqing.system.droughtinformation.mapper.DroughtCityMapper;
import com.shangqing.system.droughtinformation.mapper.DroughtGradeMapper;
import com.shangqing.system.droughtinformation.mapper.DroughtInformationMapper;
import com.shangqing.system.droughtinformation.mapper.DroughtWarningMapper;
import com.shangqing.system.droughtinformation.service.IdroughtInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class DroughtInformationServiceImpl implements IdroughtInformationService {
    @Autowired
    private DroughtInformationMapper droughtInformationMapper;
    @Autowired
    private DroughtGradeMapper droughtGrade;
    @Autowired
    private DroughtWarningMapper droughtWarning;
    @Autowired
    private DroughtCityMapper droughtCityMapper;

    @Override
    public List<Droughtinformation> selectAll() {
        List<Droughtinformation> droughtinformationList = new ArrayList<>();
        List<Droughtinformation> droughtInformation = droughtInformationMapper.selectAll();
        City city=null;
        for (Droughtinformation d : droughtInformation) {
            d.setDroughtGrade(droughtGrade.getinfoById(d.getDisastergradeid()));
            d.setDroughtWarning(droughtWarning.getAllById(d.getWarninglevelid()));
            city = droughtCityMapper.getAllCity(d.getLocationid());
            city.setCityList(cityVos(city));
            d.setCity(city);
            droughtinformationList.add(d);
        }
        return droughtinformationList;
    }
    /**
     * 递归方法体
     */
    private City cityVos(City city) {
        if (city.getParentId() == 0) {
            return null;
        } else {
            City vo = droughtCityMapper.getAllCity(city.getParentId());
            City cityVo = new City();
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCityList(cityVos(vo));
            return cityVo;
        }
    }
}
